﻿--[[
   @brief    ui logic for update_tips ui
   @author   hqzheng
   @version  1.0
   @date     2017-05-31
--]]

PREDOWNLOAD_STATE = {
    IDLE                          = 0,
    DOWNLOADING                   = 1,
    DOWNLOAD_END                  = 2,  -- 下载普通包完成
    DOWNLOAD_RECOMMEND_PKG_END    = 3  -- 下载推荐包完成
};

update_tips_control = {
    has_recommend_update = false;
    state = PREDOWNLOAD_STATE.IDLE;
    progress = "";
    result = "";
    recommend_version = "";
    build = 0;

    init = function()
        ierd_tgp.tpf_ui.Require("main_menue_update_tips\\view.lua");
        comm_center.listen_broadcast("i_broadcast_update_version_info", update_tips_control.on_get_update_info);
    end;
    
    on_open_update_tips = function(event_id, json_str)
        update_tips_view.show(update_tips_control.state);
        update_tips_control.handle_tips_content();
        -- tips打开
        update_tips_control.report_qos(21);
    end;

    parse_info = function(json_str)
        if json_str == "" then
            return;
        end;
        local info = base_utility.json_to_table(json_str);
        if info == nil then
            return;
        end;
        base_utility.log("[update_tips]parse_info, status:" .. tostring(info.status));
        local index = string.find(info.ver, ".[^.]*$");
        if index ~= nil then
            update_tips_control.build = tonumber(string.sub(info.ver, index + 1));
        end;
        update_tips_control.has_recommend_update = info.has_recommend_update;
        update_tips_control.recommend_version = info.ver;
        if ierd_tgp.game_library.kFinished == info.status then
            base_utility.log("[update_tips]parse_info, it's finished");
            update_tips_control.state = PREDOWNLOAD_STATE.DOWNLOAD_END;
            update_tips_control.result = '{"result":0}';
        end
    end;
    
    on_preupdate_detected = function(event_id, json_str)
        update_tips_control.parse_info(json_str);
    end;
    
    on_preupdate_begin = function(event_id, json_str)
    end;
    
    on_preupdate_progress = function(event_id, json_str)
        update_tips_control.state = PREDOWNLOAD_STATE.DOWNLOADING;
        update_tips_control.progress = json_str;
        update_tips_control.handle_tips_content();
    end;
    
    on_preupdate_result = function(event_id, json_str)
        base_utility.log("[update_tips]on_preupdate_result" .. tostring(json_str));
        local info = base_utility.json_to_table(json_str);
        if info == nil then
            return;
        end;
        if nil ~= info.tips_data_url and
           "" ~= info.tips_data_url then
              update_tips_control.tips_data_url = info.tips_data_url;
        end
        update_tips_control.result = json_str;
        if info.has_recommend_pkg == 1 then
            update_tips_control.state = PREDOWNLOAD_STATE.DOWNLOAD_RECOMMEND_PKG_END;
            update_tips_control.handle_tips_content();
        elseif info.pop_tips == 1 then
            update_tips_control.state = PREDOWNLOAD_STATE.DOWNLOAD_END;
            update_tips_control.handle_tips_content();
        end;
        if info.pop_tips == 1 then
            update_tips_control.on_open_update_tips(0, "");
        end
    end;
    
    handle_tips_content = function ()
        if update_tips_control.state == PREDOWNLOAD_STATE.DOWNLOADING then
            update_tips_view.update_progress(base_utility.json_to_table(update_tips_control.progress));
        elseif update_tips_control.state == PREDOWNLOAD_STATE.DOWNLOAD_END then
            update_tips_view.update_result(base_utility.json_to_table(update_tips_control.result));
        elseif update_tips_control.state == PREDOWNLOAD_STATE.DOWNLOAD_RECOMMEND_PKG_END then
            update_tips_view.update_result(base_utility.json_to_table(update_tips_control.result));
        else
            base_utility.log("[handle_tips_content]other state " .. tostring(update_tips_control.state));         
        end

        update_tips_view.switch_to_status(update_tips_control.state);
    end;
    
    report_qos = function(action)
        local qosparam = ierd_tgp.tpf_ui.LuaQosData:new();
        qosparam.oc_type = ierd_tgp.tpf_ui.kInstant;
        qosparam.qos_report_id = ierd_tgp.adapt_for_imports.qos.Qos_kind.kUrlClick; --url上报
        qosparam.int_count = 3;
        qosparam.str_count = 0; 
        qosparam.int_val[0]= action;
        qosparam.int_val[1]= update_tips_control.build;
        qosparam.int_val[2]= 0;
        base_utility.lua_qos(qosparam);
        qosparam:delete();
    end;
    
    start_predownload = function()
        local download_mgr =
            base_utility.get_component_adapter("IDownload_mgr",
                "ierd_tgp.upd_loader.GetDownloadMgrModule();");
        if download_mgr then
            local result = download_mgr:StartPreUpdate(GAME_ID.GAME_ID_TGP, update_tips_control.recommend_version);
            if false == result then
                update_tips_view.set_update_tips(LoadStr("PREUPDATE_TIPS"));
            end
        end;
    end;

    on_get_update_info = function(data)
        base_utility.log("[update_tips_control]on_get_update_info called");
        update_tips_view.update_tips_content(data);
    end;
};
update_tips_control.init();